<?php
namespace Admin\Controller;
use Think\Controller;
class RuleController extends CommonController {
    public function indexAction(){

        $sql = "select count(id) count from auth_rule";

        $count = current(M()->query($sql))['count'];

        $Page  = new \Think\Page($count,8);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show();// 分页显示输出

    	$sql = "select t1.*,t2.cname from auth_rule t1 left join auth_rule_category t2 on t1.cid=t2.id limit {$Page->firstRow},{$Page->listRows}";

    	$res = M()->query($sql);

        $sql = "select * from auth_rule_category";

        $categoryData = M()->query($sql);

        $this->assign('count',$count);
        $this->assign('categoryData',$categoryData);
        $this->assign('show',$show);
    	$this->assign('res',$res);
       	$this->display();
    }

    public function addAction()
    {


    	if(IS_POST){
    		$title = I('post.title','');
    		$name = I('post.name','');
            $cid = I('post.cid','');
    		$sql = "insert into auth_rule (name,title,cid) value('{$name}','{$title}','{$cid}')";

    		$res = M()->execute($sql);

    		if($res){
    			$this->success('增加成功',U('admin/rule/index'));
    		}else{
    			$this->error('增加失败',U('admin/rule/index'));
    		}
    	}
    }

    public function editAction()
    {
        if(IS_POST){
            $title = I('post.title','');
            $name = I('post.name','');
            $id = I('post.id','');
            $sql = "update auth_rule set title='{$title}',name='{$name}' where id='{$id}'";

            $res = M()->execute($sql);

            if($res){
                $this->success('修改成功',U('admin/rule/index'));
            }else{
                $this->error('修改失败',U('admin/rule/index'));
            }
        }else{
           $id = I('get.id','');
           if(empty($id)){
               $this->error('非法请求',U('admin/rule/index')); 
           }else{
                $sql = "select * from auth_rule where id = '{$id}'";
                $res = M()->query($sql);
                $this->assign('res',current($res));
                $this->display();
           }
        }
    	
    }

    public function delAction()
    {
        $id = I('get.id');

        $sql = "delete from  auth_rule where id='{$id}'";

        $res = M()->execute($sql);

        if($res){
            $this->success('删除成功',U('admin/rule/index'));
        }else{
            $this->error('删除失败',U('admin/rule/index'));
        }
    }

    public function categoryAction()
    {

        
        $sql = "select count(id) count from auth_rule_category";

        $count = current(M()->query($sql))['count'];

        $Page  = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show();// 分页显示输出

        $sql = "select * from auth_rule_category limit {$Page->firstRow},{$Page->listRows}";

        $res = M()->query($sql);

        $this->assign('count',$count);
        $this->assign('show',$show);
        $this->assign('res',$res);
        $this->display();
    }

}